package com.otv.core;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CoreHelper2 {

	// List of time servers: http://tf.nist.gov/service/time-servers.html
	// Do not query time server more than once every 4 seconds
	public static final String TIME_SERVER = "time-a.nist.gov";

	public static Date getInternetTime() throws Exception {
		String query = "http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo&style=full";

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		// parse the response
		Document document = builder.parse(query);
		// get the tag with ID="Title"
		NodeList list = document.getElementsByTagName("time");
		String timeString = null;
		for (int i = 0; i < list.getLength(); i++) {
			Node node = list.item(i);
			// print out the content
			timeString = node.getTextContent();

			System.out.println(timeString);
			break;

		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd hh:mm");
		Date parse = simpleDateFormat.parse(timeString);
		return parse;
	}

	public static void main(String[] args) throws Exception {
		Date internetTime = getInternetTime();
	}

	public static Date getOsTime() {
		Date date = new Date(System.currentTimeMillis());
		return date;
	}

	public static Date getTrialIsOver() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, 2012);
		calendar.set(Calendar.MONTH, 9);
		calendar.set(Calendar.DAY_OF_MONTH, 03);

		Date timeIsUp = calendar.getTime();

		return timeIsUp;
	}
}